package com.android.goal;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class GoalActivity extends Activity implements OnClickListener {	

	ArrayList<League> listLeague;
	LeagueAdapter adapter;
	
	Date date = new Date();
	
	// tao form de hien thi tren man hinh
	Format formDateView = new SimpleDateFormat("dd/MM/yyyy");
	
	// tao form de gui request den service
	Format formDateGetLink = new SimpleDateFormat("yyyy-MM-dd");
	
	TextView todayView,  notificationView;
	ImageView yesterdayView, tommorowView;
	ListView list;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.live_score);
		
		this.date.setDate(this.date.getDate()-12);
		// ngay hien tai tru di 
		// do db chua co thong tin lien tuc
		
		todayView = (TextView) this.findViewById(R.id.today_live_score);
		yesterdayView = (ImageView) this.findViewById(R.id.yesterday_live_score);
		tommorowView = (ImageView) this.findViewById(R.id.tomorrow_live_score);
		notificationView = (TextView) this.findViewById(R.id.notification_live_score);
		
		yesterdayView.setOnClickListener(this);
		tommorowView.setOnClickListener(this);
		
		listLeague = new ArrayList<League>();																							
		adapter = new LeagueAdapter(this, R.layout.leagues_view,listLeague);		
		list = (ListView) findViewById(R.id.list_leagues);
		list.setAdapter(adapter);
		this.display();
	}	
	
	public void display(){
		todayView.setText(formDateView.format(date));
		this.adapter.clear();
		this.listLeague.clear();
		String input = Utility.getHttpMatch(formDateGetLink.format(date));		
		input = input.substring(1,input.length()-1);
		
		if (input.equals("")){
			notificationView.setText("No match");
		}
		else{						
			notificationView.setText("");
			this.parseJson(input);			
		}											
	}
	
	public void parseJson(String json){
		try{				
			// chuoi json input la 1 mang cac tran dau
			JSONArray jsonArray = new JSONArray(json);	
			Map<String, League> mapLeague = new HashMap<String, League>();
			// moi object la 1 chuoi json ve thong tin tran dau
			for (int i = 0; i < jsonArray.length(); i++){
				// lay chuoi json ve thong tin tran dau thu i
				JSONObject matchJson = jsonArray.getJSONObject(i);				
				Match match = new Match(matchJson);
				// bat thong tin ve ten giai dau trong object 																					    
				String competition = matchJson.getString("competition");
				League l = new League(competition);								
				if (!mapLeague.containsKey(competition)){
					mapLeague.put(competition, l);					
				}					
				mapLeague.get(competition).addMatch(match);
			}	
			
			for (String key:mapLeague.keySet()){
				League l = mapLeague.get(key);
				this.listLeague.add(l);				
			}
			this.adapter.notifyDataSetChanged();			
		}catch(NumberFormatException e){
			System.out.println("Could not parse " + e);
		}catch(JSONException e){
			e.printStackTrace();			
		}
	}

	public void onClick(View view) {
		// TODO Auto-generated method stub
		int id = view.getId();
		if (id == R.id.yesterday_live_score)
			this.date.setDate(this.date.getDate()-1);							
		else if (id == R.id.tomorrow_live_score)
			this.date.setDate(this.date.getDate()+1);
		this.display();
	}			
}
